package cn.glc.daily.life.config.shiro;

import cn.glc.daily.life.config.shiro.filter.JwtFilter;
import cn.glc.daily.life.config.shiro.realm.JwtTokenRealm;
import com.google.common.collect.Maps;
import org.apache.shiro.mgt.SubjectFactory;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Map;

/**
 * @author simon
 * @date 2020/7/7 11:37
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Configuration
public class ShiroConfig {

    @Bean
    public CustomHashedCredentialsMatcher customHashedCredentialsMatcher() {
        return new CustomHashedCredentialsMatcher();
    }

    @Bean
    public JwtTokenRealm createJwtTokenRealmBean() {
        JwtTokenRealm jwtTokenRealm = new JwtTokenRealm();
        jwtTokenRealm.setCredentialsMatcher(customHashedCredentialsMatcher());
        return jwtTokenRealm;
    }

    @Bean
    public SubjectFactory subjectFactory() {
        return new DefaultWebSubjectFactory() {
            @Override
            public Subject createSubject(SubjectContext context) {
                context.setSessionCreationEnabled(false);
                return super.createSubject(context);
            }
        };
    }

    /**
     * 权限管理，配置主要是Realm的管理认证
     *
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(createJwtTokenRealmBean());
        return securityManager;
    }

    /**
     * Filter工厂，设置对应的过滤条件和跳转条件
     *
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, Filter> filter = Maps.newLinkedHashMapWithExpectedSize(1);
        filter.put("jwtFilter", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filter);

        Map<String, String> filterMap = Maps.newLinkedHashMapWithExpectedSize(4);
        // swagger
        filterMap.put("/swagger**/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/webjars/springfox-swagger-ui/**", "anon");
        filterMap.put("/configuration/ui", "anon");
        filterMap.put("/configuration/security", "anon");
        filterMap.put("/images/**", "anon");
        // 登录/统一登录
        filterMap.put("/login", "anon");
        filterMap.put("/unifiedLogin", "anon");
        // 登出
        filterMap.put("/logout", "anon");
        // 用户注册
        filterMap.put("/signup", "anon");
        // 第三方token回调地址
        filterMap.put("/thirdToken/**", "anon");
        filterMap.put("/user/sendCode", "anon");
        // 对所有用户认证
        filterMap.put("/**", "jwtFilter");
        // 错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 加入注解的使用，不加入这个注解不生效
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }
}
